package bstsetcode.linkedliststack;

/**
 * 给链表添加一个虚拟头结点dummyHead
 * @author noob
 * @version 1.0
 * @date 2021/3/15 8:01
 */
public class LinkedList2<E> {

    private class Node{
        E e;
        Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e){
            this(e,null);
        }

        public Node() {
            this(null,null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }




    private Node dummyHead;
    private int size;


    public LinkedList2() {
        this.dummyHead=new Node(null,null);
        this.size = 0;
    }



    //获取链表中元素的个数
    public int getSize(){
        return size;
    }

    //判断链表是否为空
    public boolean isEmpty(){
        return size==0;
    }


    //在链表头部添加新的元素
    public void addFirst(E e){
        add(0,e);
    }

    //在链表的index(0-based)位置添加新的元素e
    //找到待插入结点的前一个位置
    public void  add(int index,E e){
        if (index<0 || index> size){
            throw new IllegalArgumentException("add failed,Illegal index");
        }


        Node prev = dummyHead;
        //找到index前面的那个节点
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
//        Node node = new Node(e);
//        node.next = prev.next;
//        prev.next = node;
        prev.next = new Node(e,prev.next);
        size++;


    }


    //在链表的末尾添加元素
    public void addLast(E e){
        add(size,e);
    }

    /**
     * 获取值：索引要找到index的位置
     * @param index
     * @return
     */
    public E getElementByIndex(int index){
        if (index<0 || index> size){
            throw new IllegalArgumentException("get failed,Illegal index");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return  cur.e;

    }

    public E getFirst(){
       return  getElementByIndex(0);
    }

    public E getLast(){
        return  getElementByIndex(size-1);
    }

//    修改链表的第index个位置的值
    public void set(int index,E e){
        if (index<0 || index> size){
            throw new IllegalArgumentException("set failed,Illegal index");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur=cur.next;
        }

        cur.e = e;
    }

//    查找链表中是否存在元素e
    public boolean contains(E e){
        Node cur = dummyHead.next;
        while (cur !=null){
            if(cur.e.equals(e)){
                return true;
            }
            cur = cur.next;
        }

        return false;
    }


    //删除指定索引的元素
    public E remove(int index){
        if (index<0 || index> size){
            throw new IllegalArgumentException("remove failed,Illegal index");
        }

        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node oldNode = prev.next;
        prev.next=oldNode.next;
        oldNode.next=null;
        size--;
        return oldNode.e;
    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size-1);
    }



    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null){
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("Null");
        return  res.toString();
    }
}
